package v2.chapter2.fileOp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.stream.Stream;

public class PathTest {

	public static void main(String[] args) throws IOException {
		test3();
	}

	private static void test3() throws IOException {
		String pathStr = "D:\\java\\jdk1.8.0_172";
		try (Stream<Path> entries = Files.list(Paths.get(pathStr))) {
			entries.forEach(s -> System.out.println(s));
		}

		try (Stream<Path> entries = Files.walk(Paths.get(pathStr))) {
			OutputStream output = new FileOutputStream("F:\\jdkPaths.txt");
			entries.forEach(s -> {
				byte data[] = s.toString().getBytes();
				try {
					output.write(data);
					output.write("\r\n".getBytes());
				} catch (IOException e) {
					e.printStackTrace();
				}
			});
			output.close();
		}

		// try (DirectoryStream<Path> entries =
		// Files.newDirectoryStream(Paths.get(pathStr, "*.txt"))) {
		// for (Path entry : entries)
		// System.out.println(entry);
		// }

		/*
		 * Files.walkFileTree(Paths.get(pathStr), new SimpleFileVisitor<Path>() { public
		 * FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws
		 * IOException { System.out.println(dir); return FileVisitResult.CONTINUE; }
		 * 
		 * public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws
		 * IOException { return FileVisitResult.CONTINUE; }
		 * 
		 * public FileVisitResult visitFileFailed(Path file, IOException exc) throws
		 * IOException { return FileVisitResult.CONTINUE; } });
		 */

		FileSystem fs = FileSystems.newFileSystem(Paths.get("D:\\java\\jdk1.8.0_172\\javafx-src.zip"), null);

		Files.walkFileTree(fs.getPath("/"), new SimpleFileVisitor<Path>() {
			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
				System.out.println(file);
				return FileVisitResult.CONTINUE;
			}
		});
	}

	// 读写文件
	private static void test2() {
		try {
			byte[] bytes = Files.readAllBytes(Paths.get("cities.txt"));
			String content = new String(bytes);
			System.out.println(content);

			List<String> lines = Files.readAllLines(Paths.get("cities.txt"));
			Iterator<String> itr = lines.iterator();

			while (itr.hasNext()) {
				System.out.println(itr.next());
			}

			BasicFileAttributes attributes = Files.readAttributes(Paths.get("cities.txt"), BasicFileAttributes.class);

			System.out.println(attributes.isSymbolicLink());
			System.out.println(attributes.lastAccessTime());
			System.out.println(attributes.isDirectory());
			System.out.println(attributes.creationTime());
			System.out.println(attributes.lastModifiedTime());

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	// 1. Path
	private static void test1() {

		Path absolute = Paths.get("C:\\Windows", "Fonts");
		System.out.println(absolute.toFile().getPath());
		Path relative = Paths.get("v2", "chapter2", "fileOp");
		System.out.println(relative.toFile().getPath());

		String baseDir = System.getProperty("user.dir");
		System.out.println(baseDir);

		Path basePath = Paths.get(baseDir);
		System.out.println(basePath.toString());

		Path workRelative = Paths.get("v2");
		System.out.println(workRelative);
		Path workPath = basePath.resolve(workRelative);
		System.out.println(workPath);
		Path tempPath = workPath.resolveSibling("v3/fileOp");
		System.out.println(tempPath);

		Path p = Paths.get(baseDir);
		Path parent = p.getParent();
		System.out.println(parent);
		Path file = p.getFileName();
		System.out.println(file);
		Path root = p.getRoot();
		System.out.println(root);

		try {
			Scanner in = new Scanner(Paths.get("cities.txt"));
			while (in.hasNext()) {
				System.out.println(in.nextLine());
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
